/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon;

import org.apache.log4j.Logger;
import za.org.foobar.famon.util.Util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Carl Crous
 */
public class DeleteUnusedFiles {
    private static final Logger log = Logger.getLogger(DeleteUnusedFiles.class);
//    private static FilenameFilter filter;
    private static Map<String, String> map;

    private static String path = "../../../exploded/famon".replace('/', File.separatorChar);
    private static File baseDir = new File(path);

    public static void main(String args[]) throws IOException {
        //log.info("Local: " + new File("").getAbsolutePath());
        File usedFilesTxt = new File("UsedFiles.txt");

        File dir = new File(baseDir, "sc");
        log.info("Base: " + baseDir.getAbsolutePath());
        log.info("usedFilesTxt: " + usedFilesTxt.getAbsolutePath() + ", exists " + usedFilesTxt.exists());


        log.info("Deleting Unused File");
        String usedFiles = Util.readFile(usedFilesTxt);
        String usedFilesArr[] = usedFiles.split("\n");

        map = new HashMap<String, String>();
        for (String f : usedFilesArr) {
            f = f.trim().replace('/', File.separatorChar);
            log.info("File: " + f);
            map.put(f, f);
        }

//        filter = new FilenameFilter() {
//            public boolean accept(File dir, String name) {
//                return !name.startsWith(".");
//            }
//        };

        check(dir);
    }

    public static int check(File dir) {
        File children[] = dir.listFiles();
        int cnt = 0;
        for (File f : children) {
            if (f.isDirectory()) {
                cnt += check(f);
            } else {
                String path = f.getAbsolutePath().substring(baseDir.getAbsolutePath().length()).trim();
                if (map.get(path) != null) {
                    log.info("File Used: " + path);
                    cnt += 1;
                } else {
                    log.info("Deleting: " + path);
                    if (!f.delete()) {
                        log.error("Could not delete: " + path);
                    }
                }
            }
        }
        if (cnt == 0) {
            log.info("Deleting Dir: " + dir);
            if (!dir.delete()) {
                log.error("Could not delete empty directory: " + dir.getAbsolutePath());
            }
        }
        return cnt;
    }
}
